<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MakeStockoutPaper
 *
 * @author 志鹏
 */
class MakeStockoutPaper extends WorkflowAbstract {
    
    public function run() {
        
        $order = D("Orders")->find($this->mainrowId);
        $stockout = D("Stockout");
        $data = array(
            "source_id" => $this->mainrowId,
            "source_model" => "Orders",
            "stock_id" => $order["stock_id"],
            "dateline" => CTS,
            "source_user_id" => $order["saler_id"]
        );
        $stockout->startTrans();
        
        $stockoutId = $stockout->add($data);
        
        
        $stockoutDetail = D("StockoutDetail");
        $orderDetail = D("OrdersDetail");
        $details = $orderDetail->where("order_id=".$this->mainrowId)->select();
        if(!$details) {
            //@todo
            return;
        }
        foreach($details as $d) {
            $data = array(
//                "source_id" => $this->mainrowId,
                "stockout_id" => $stockoutId,
                "source_row_id" => $d["id"]
            );
            $rs = $stockoutDetail->add($data);
            if(!$rs) {
                $stockout->rollBack();
                break;
            }
        }
        
        if(!$stockoutId) {
            echo "failed";exit;
            $stockout->rollBack();
        } else {
            $stockout->commit();
        }
    }
    
}

?>
